/*
 * Created by JFormDesigner on Sat Nov 02 15:05:30 CST 2024
 */

package com.dhx.view;

import com.dhx.service.UserService;
import com.dhx.util.StringUtil;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author 25188
 */
public class LoginFrm extends JFrame {
    //引入业务层UserService
    private UserService userService = new UserService();
    public LoginFrm() {
        initComponents();
    }

    private void button1(ActionEvent e) {
        // TODO add your code here  登录事件
        //获取用户名
        String username = textField1.getText();
        //获取密码
        String password = new String(passwordField1.getPassword());
        //判断用户名是否为空
        if(StringUtil.isEmpty(username)){
            JOptionPane.showMessageDialog(null,"用户名不能为空！");
            return;
        }
        //判断密码是否为空
        if(StringUtil.isEmpty(password)){
            JOptionPane.showMessageDialog(null,"密码不能为空！");
            return;
        }
        Boolean isLogin = userService.login(username,password);
        if(isLogin){
            //登录成功
            JOptionPane.showMessageDialog(null,"登录成功！");
            //关闭当前窗口
            this.dispose();
            //打开主界面
            MainFrm mainFrm = new MainFrm();
            mainFrm.setVisible(true);
        }else{
            //登录失败
            JOptionPane.showMessageDialog(null,"用户名或密码错误！");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        textField1 = new JTextField();
        passwordField1 = new JPasswordField();
        button1 = new JButton();

        //======== this ========
        setResizable(false);
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u6cb3\u5357\u8d22\u653f\u91d1\u878d\u5b66\u9662\u56fe\u4e66\u7ecf\u9500\u5b58\u7cfb\u7edf");
        label1.setFont(label1.getFont().deriveFont(Font.BOLD, label1.getFont().getSize() + 5f));

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");

        //---- label3 ----
        label3.setText("\u5bc6  \u7801");

        //---- button1 ----
        button1.setText("\u767b\u9646");
        button1.addActionListener(e -> button1(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(143, 143, 143)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(21, 21, 21)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label2)
                                .addComponent(label3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGap(34, 34, 34)
                                    .addComponent(button1))
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(passwordField1, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE))))
                            .addGap(33, 33, 33)))
                    .addGap(210, 210, 210))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(15, 15, 15)
                    .addComponent(label1)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(73, 73, 73)
                            .addComponent(label2)
                            .addGap(35, 35, 35)
                            .addComponent(label3, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
                            .addGap(35, 35, 35))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(35, 35, 35)
                            .addComponent(passwordField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(43, 43, 43)))
                    .addComponent(button1)
                    .addContainerGap(61, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField1;
    private JPasswordField passwordField1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
